// ==========================================================================
// 云捷GO自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-08-17 14:40:38
// 生成路径: app/service/module/attachment/attachment_service.go
// 生成人：gfcms
// ==========================================================================
package attachment

import (
	"fmt"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	"os"
	attachmentModel "myblog/app/model/module/attachment"
	userService "myblog/app/service/system/user"
	"myblog/app/utils/convert"
	"myblog/app/utils/excel"
	"myblog/app/utils/page"
)

//根据主键查询数据
func SelectRecordById(id int64) (*attachmentModel.Entity, error) {
	return attachmentModel.FindOne("id", id)
}

//根据主键删除数据
func DeleteRecordById(id int64) bool {
	result, err := attachmentModel.Delete("id", id)
	if err == nil {
		affected, _ := result.RowsAffected()
		if affected > 0 {
			return true
		}
	}

	return false
}

//批量删除数据记录
func DeleteRecordByIds(ids string) int64 {
	idarr := convert.ToInt64Array(ids, ",")
	//批量删除文件
	res, _ := attachmentModel.FindAll("id in (?)", idarr)
	for _, v := range res {
		curDir, _ := os.Getwd()
		err := os.Remove(curDir + "/public/upload/" + v.Filename)
		if err != nil {
			fmt.Printf("%v", err)
		}
	}
	result, err := attachmentModel.Delete("id in (?)", idarr)
	if err != nil {
		return 0
	}

	nums, _ := result.RowsAffected()

	return nums
}

//添加数据
func AddSave(req *attachmentModel.AddReq, session *ghttp.Session) (int64, error) {
	var entity attachmentModel.Entity

	entity.Filename = req.Filename
	entity.Filepath = req.Filepath
	entity.Filesize = req.Filesize
	entity.Imagewidth = req.Imagewidth
	entity.Imageheight = req.Imageheight
	entity.CreateTime = gtime.Now()
	entity.CreateBy = ""

	user := userService.GetProfile(session)

	if user != nil {
		entity.CreateBy = user.LoginName
	}

	result, err := entity.Insert()
	if err != nil {
		return 0, err
	}

	id, err := result.LastInsertId()

	if err != nil || id <= 0 {
		return 0, err
	}
	return id, nil
}

//修改数据
func EditSave(req *attachmentModel.EditReq, session *ghttp.Session) (int64, error) {

	entity, err := attachmentModel.FindOne("id=?", req.Id)

	if err != nil {
		return 0, err
	}

	if entity == nil {
		return 0, gerror.New("数据不存在")
	}

	entity.Filename = req.Filename
	entity.Filepath = req.Filepath
	entity.Filesize = req.Filesize
	entity.Imagewidth = req.Imagewidth
	entity.Imageheight = req.Imageheight
	entity.UpdateTime = gtime.Now()
	entity.UpdateBy = ""

	user := userService.GetProfile(session)

	if user == nil {
		entity.UpdateBy = user.LoginName
	}

	result, err := entity.Save()

	if err != nil {
		return 0, err
	}

	rs, err := result.RowsAffected()

	if err != nil {
		return 0, err
	}

	return rs, nil
}

//根据条件查询数据
func SelectListAll(params *attachmentModel.SelectPageReq) ([]attachmentModel.Entity, error) {
	return attachmentModel.SelectListAll(params)
}

//根据条件分页查询数据
func SelectListByPage(params *attachmentModel.SelectPageReq) ([]attachmentModel.Entity, *page.Paging, error) {
	return attachmentModel.SelectListByPage(params)
}

// 导出excel
func Export(param *attachmentModel.SelectPageReq) (string, error) {
	result, err := attachmentModel.SelectListExport(param)
	if err != nil {
		return "", err
	}

	head := []string{"", "文件名", "文件路径", "文件大小", "图片宽度", "图片高度", "", "", "", ""}
	key := []string{"id", "filename", "filepath", "filesize", "imagewidth", "imageheight", "create_time", "update_time", "update_by", "create_by"}
	url, err := excel.DownlaodExcel(head, key, result)

	if err != nil {
		return "", err
	}

	return url, nil
}
